package com.develop.data.loan.jpa.attribute.converter;

import com.develop.data.loan.option.constraint.CodedOption;
import net.bytebuddy.implementation.bind.annotation.FieldValue;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;

public class CodedEnumAttributeConverterInterceptor {

    private CodedEnumAttributeConverterInterceptor() {}

    @RuntimeType
    public static <OptionType extends Enum<?> & CodedOption> String convertToDatabaseColumn(OptionType constant) {
        return constant == null ? null : constant.getCode();
    }

    @RuntimeType
    public static <OptionType extends Enum<?> & CodedOption> OptionType convertToEntityAttribute(String code, @FieldValue("targetType") Class<OptionType> targetType) {
        CodedEnumAttributeConverterFactory factory = new CodedEnumAttributeConverterFactory();
        CodedEnumAttributeConverter<OptionType> converter = factory.getCodedEnumAttributeConverter(targetType);
        return converter.convertToEntityAttribute(code);
    }
}
